package cn.wolfcode.mq;

import cn.wolfcode.service.ISeckillProductService;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.annotation.SelectorType;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 对失败的订单进行消费，同时做一些操作：打开秒杀服务的请求访问.
 */
@Component
@RocketMQMessageListener(consumerGroup = "orederResultMessageFailConsumer",
                        topic = MQConstant.ORDER_RESULT_TOPIC,
                        selectorType = SelectorType.TAG,
                        selectorExpression = MQConstant.ORDER_RESULT_FAIL_TAG)
public class MyOrderResultFailListener  implements RocketMQListener<OrderMQResult> {

    @Autowired
    private ISeckillProductService seckillProductService;
    @Autowired
    private RocketMQTemplate rocketMQTemplate;
    @Override
    public void onMessage(OrderMQResult orderMQResult) {
        //1、回补此秒杀商品的预库存（采用从mysql数据库查出来缓存到redis）
        seckillProductService.initPreCountInRedis(orderMQResult.getTime(),orderMQResult.getSeckillId());
        //2、修改本地标识（采用rocketMQ对各个本地标识进行统一的一个处理）
        //消息为orderMQResult:里面有下一步修改本地标识业务所需数据
        rocketMQTemplate.sendOneWay(MQConstant.CANCEL_SECKILL_OVER_SIGE_TOPIC,orderMQResult);
    }
}
